package com.pjedu.nbmalladminsystem.web;


import java.util.HashMap;

import com.pjedu.nbmallcommon.common.MallCategoryLevelEnum;
import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.Result;
import com.pjedu.nbmallcommon.utils.ResultGenerator;
import com.pjedu.nbmallshop.entity.MallGoodsCategory;
import com.pjedu.nbmallshop.service.MallGoodsCategoryService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**这里是类的注释*/
@RestController
@RequestMapping("/admin")
public class MallGoodsCategoryController {

    @Resource
    private MallGoodsCategoryService mallGoodsCategoryService;

    /**
     * 列表
     */
    @RequestMapping(value = "/categories/list", method = RequestMethod.GET)
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit")) || StringUtils.isEmpty(params.get("categoryLevel")) || StringUtils.isEmpty(params.get("parentId"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        HashMap pageUtil = new HashMap(params);
        return ResultGenerator.genSuccessResult(mallGoodsCategoryService.getCategorisPage(pageUtil));
    }

    /**
     * 根据所选择的大类，关联出相关分类
     */
    @RequestMapping(value = "/categories/listForSelect", method = RequestMethod.GET)
    public Result listForSelect(@RequestParam("categoryId") Long categoryId) {
        Map categoryResult = new HashMap(3);

        // 不传参，默认返回一级分类
        if (categoryId == null || categoryId < 1) {
            List<MallGoodsCategory> firstLevelCategories =
                    mallGoodsCategoryService.selectByLevelAndParentIdsAndNumber
                            (Collections.singletonList(0L), MallCategoryLevelEnum.LEVEL_ONE.getLevel());
            categoryResult.putIfAbsent("firstLevelCategories", firstLevelCategories);
            return ResultGenerator.genSuccessResult(categoryResult);
        }

        MallGoodsCategory category = mallGoodsCategoryService.getGoodsCategoryById(categoryId);

        //如果是一级分类则返回当前一级分类下的所有二级分类，以及二级分类列表中第一条数据下的所有三级分类列表
        if (category.getCategoryLevel() == MallCategoryLevelEnum.LEVEL_ONE.getLevel()) {
            //查询一级分类列表中第一个实体的所有二级分类
            List<MallGoodsCategory> secondLevelCategories = mallGoodsCategoryService.selectByLevelAndParentIdsAndNumber(Collections.singletonList(categoryId), MallCategoryLevelEnum.LEVEL_TWO.getLevel());
            categoryResult.put("secondLevelCategories", secondLevelCategories);
        }

        //如果是二级分类则返回当前分类下的所有三级分类列表
        if (category.getCategoryLevel() == MallCategoryLevelEnum.LEVEL_TWO.getLevel()) {
            List<MallGoodsCategory> thirdLevelCategories = mallGoodsCategoryService.selectByLevelAndParentIdsAndNumber(Collections.singletonList(categoryId), MallCategoryLevelEnum.LEVEL_THREE.getLevel());
            categoryResult.put("thirdLevelCategories", thirdLevelCategories);
        }

        //如果是三级分类，则反向查询出归属的分类信息
        if (category.getCategoryLevel() == MallCategoryLevelEnum.LEVEL_THREE.getLevel()) {
            //查询同级别的所有三级分类
            List<MallGoodsCategory> thirdLevelCategories =
                    mallGoodsCategoryService.selectByLevelAndParentIdsAndNumber
                            (Collections.singletonList(category.getParentId()), MallCategoryLevelEnum.LEVEL_THREE.getLevel());
            // 查询同级别的所有二级分类
            MallGoodsCategory secondLevelCategory = mallGoodsCategoryService.getGoodsCategoryById(category.getParentId());
            List<MallGoodsCategory> secondLevelCategories = mallGoodsCategoryService.selectByLevelAndParentIdsAndNumber(Collections.singletonList(secondLevelCategory.getParentId()), MallCategoryLevelEnum.LEVEL_TWO.getLevel());
            // 查询所有一级分类
            List<MallGoodsCategory> firstLevelCategories =
                    mallGoodsCategoryService.selectByLevelAndParentIdsAndNumber
                            (Collections.singletonList(0L), MallCategoryLevelEnum.LEVEL_ONE.getLevel());

            // 要返回的对象
            categoryResult.putIfAbsent("firstLevelCategories", firstLevelCategories);
            categoryResult.put("secondLevelCategories", secondLevelCategories);
            categoryResult.put("thirdLevelCategories", thirdLevelCategories);
            categoryResult.put("firstLevelSelectId", secondLevelCategory.getParentId());
            categoryResult.put("secondLevelSelectId", category.getParentId());
            categoryResult.put("thirdLevelSelectId", category.getCategoryId());
        }
        return ResultGenerator.genSuccessResult(categoryResult);
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/categories/save", method = RequestMethod.POST)
    public Result save(@RequestBody MallGoodsCategory goodsCategory) {
        if (Objects.isNull(goodsCategory.getCategoryLevel())
                || StringUtils.isEmpty(goodsCategory.getCategoryName())
                || Objects.isNull(goodsCategory.getParentId())
                || Objects.isNull(goodsCategory.getCategoryRank())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = mallGoodsCategoryService.saveCategory(goodsCategory);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/categories/update", method = RequestMethod.POST)
    public Result update(@RequestBody MallGoodsCategory goodsCategory) {
        if (Objects.isNull(goodsCategory.getCategoryId())
                || Objects.isNull(goodsCategory.getCategoryLevel())
                || StringUtils.isEmpty(goodsCategory.getCategoryName())
                || Objects.isNull(goodsCategory.getParentId())
                || Objects.isNull(goodsCategory.getCategoryRank())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = mallGoodsCategoryService.updateGoodsCategory(goodsCategory);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 详情
     */
    @GetMapping("/categories/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        MallGoodsCategory goodsCategory = mallGoodsCategoryService.getGoodsCategoryById(id);
        if (goodsCategory == null) {
            return ResultGenerator.genFailResult("未查询到数据");
        }
        return ResultGenerator.genSuccessResult(goodsCategory);
    }

    /**
     * 分类删除
     */
    @RequestMapping(value = "/categories/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (mallGoodsCategoryService.deleteBatch(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }
}